::
:: Licensed under the Apache License, Version 2.0 (the "License");
:: you may not use this file except in compliance with the License.
:: You may obtain a copy of the License at
:: 
::     http://www.apache.org/licenses/LICENSE-2.0
:: 
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.
:: 

::
:: Generates a Visual Studio solution for thrift and then builds it.
:: Assumes third party libraries have been built or placed already.
::
:: Open a Visual Studio Command Prompt of your choosing and then
:: run this script.  
::
:: Normally the script will run cmake to generate a solution, then
:: perform a build, then run tests on the complete thrift library
:: in release mode.
::
:: Flags you can use to change this behavior:
::
::   /DEBUG            - debug instead of release
::   /NOBUILD          - skip cmake build - useful if you just
::                       want to generate a solution
::   /MD               - build with /MD option,default:/MT
::   /COMPILER         - build thrift compiler only
::

@ECHO OFF
SETLOCAL EnableDelayedExpansion
set sh_folder=%~dp0
SET PACKAGE_NAME=thrift

:: Defaults
CALL %sh_folder%thrift\build\wincpp\scripts\tpversions.bat || EXIT /B

:: Set COMPILER to (vc100 - vc140) depending on the current environment
CALL %sh_folder%thrift\build\wincpp\scripts\cl_setcompiler.bat || EXIT /B

:: Set ARCH to either win32 or x64 depending on the current environment
CALL %sh_folder%thrift\build\wincpp\scripts\cl_setarch.bat || EXIT /B

:: Set GENERATOR for CMake depending on the current environment
CALL %sh_folder%thrift\build\wincpp\scripts\cl_setgenerator.bat || EXIT /B

:: ONLY FOR BUILD COMPILER
SET BISON_EXE=%sh_folder%\dist\winflexbison\win_bison.exe
SET FLEX_EXE=%sh_folder%\dist\winflexbison\win_flex.exe

SET OPT_BUILD=1
SET OPT_MT=ON
SET OPT_COMPILER=OFF

:: parse command arguments
:loop
IF x%1 == x goto :pare_end
IF /I "%1" == "/NOBUILD"      SET OPT_BUILD=0
IF /I "%1" == "/MD"           SET OPT_MT=OFF
IF /I "%1" == "/COMPILER"     SET OPT_COMPILER=ON
SHIFT
goto :loop
:pare_end

IF /I %OPT_COMPILER% == ON    (
  SET OPT_MT=ON
)

SET BUILDDIR=%sh_folder%build\%PACKAGE_NAME%-%COMPILER%-%ARCH%\
SET OUTDIR=%sh_folder%dist\%PACKAGE_NAME%-%COMPILER%-%ARCH%\

ECHO/
ECHO =========================================================================
ECHO     Configuration: %PACKAGE_NAME% %COMPILER%:%ARCH%:"%GENERATOR%"
ECHO   Build Directory: %BUILDDIR%
ECHO Install Directory: %OUTDIR%
ECHO  Source Directory: %SOURCEDIR%
ECHO =========================================================================
ECHO/

IF EXIST "%BUILDDIR%" ( RMDIR "%BUILDDIR%" /s/q || EXIT /B )
MKDIR "%BUILDDIR%" || EXIT /B
CD "%BUILDDIR%" || EXIT /B

CMAKE.EXE %sh_folder%thrift ^
    -G"%GENERATOR%" ^
    -DCMAKE_INSTALL_PREFIX=%OUTDIR% ^
    -DBISON_EXECUTABLE=%BISON_EXE% ^
    -DFLEX_EXECUTABLE=%FLEX_EXE% ^
    -DBOOST_ROOT=%sh_folder%\dist\boost_%TP_BOOST_VERSION% ^
    -DBOOST_LIBRARYDIR=%sh_folder%\dist\boost_%TP_BOOST_VERSION%\%BOOST_LIBDIR% ^
    -DOPENSSL_ROOT_DIR=%sh_folder%\dist\openssl-%TP_OPENSSL_VERSION%\%COMPILER%\%ARCH%\release\static ^
    -DOPENSSL_USE_STATIC_LIBS=ON ^
    -DZLIB_LIBRARY=%sh_folder%\dist\zlib-%TP_ZLIB_VERSION%\%COMPILER%\%ARCH%\lib\zlibstatic%ZLIB_LIB_SUFFIX%.lib ^
    -DZLIB_ROOT=%sh_folder%\dist\zlib-%TP_ZLIB_VERSION%\%COMPILER%\%ARCH% ^
    -DWITH_OPENSSL=ON ^
    -DWITH_SHARED_LIB=OFF ^
    -DWITH_MT=%OPT_MT% ^
    -DWITH_JAVA=OFF ^
    -DWITH_PYTHON=OFF ^
    -DWITH_LIBEVENT=OFF ^
    -DWITH_ZLIB=ON ^
    -DBUILD_COMPILER=%OPT_COMPILER% ^
    -DBUILD_TESTING=OFF ^
    -DBUILD_EXAMPLES=OFF ^
    -DBUILD_TUTORIALS=OFF ^
    -DWITH_STATIC_LIB=ON || EXIT /B


IF %OPT_BUILD% == 1 (

  CD %BUILDDIR%
  IF /I %OPT_COMPILER% == ON    (
    CMAKE.EXE --build . --config Release --target thrift-compiler || EXIT /B
    XCOPY /F /Y %BUILDDIR%compiler\cpp\bin\Release\thrift.exe %OUTDIR%bin\
  ) ELSE (
    CMAKE.EXE --build . --config Release --target install || EXIT /B
    CMAKE.EXE --build . --config Debug   --target install || EXIT /B
  )
)

ENDLOCAL
EXIT /B
